package edu.nf.bs.service.impl;

import com.github.pagehelper.PageInfo;
import edu.nf.bs.dao.UserInfoMapper;
import edu.nf.bs.dao.impl.UserInfoMapperImpl;
import edu.nf.bs.entity.UserInfo;
import edu.nf.bs.exception.AddException;
import edu.nf.bs.exception.LoginException;
import edu.nf.bs.exception.UploadPictureException;
import edu.nf.bs.service.UserInfoService;
import edu.nf.bs.util.HttpUtil;


import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;

public class UserInfoServiceImpl implements UserInfoService {
    private UserInfoMapper userInfoMapper;

    public void setUserInfoMapper(UserInfoMapper userInfoMapper) {
        this.userInfoMapper = userInfoMapper;
    }

    @Override
    public UserInfo checkUser(UserInfo userInfo) {
        UserInfo userByPhone = userInfoMapper.getUserByPhone(userInfo.getUserPhone());
        if(userByPhone!=null){
            if(userInfo.getUserPassword().equals(userByPhone.getUserPassword())){
                    return userByPhone;
            }
            throw new LoginException("用户名或密码错误");
        }
        throw new LoginException("用户名或密码错误");
    }

    @Override
    public PageInfo<UserInfo> listUser(String userPhone, Integer pageNum, Integer pageSize) {
        List<UserInfo> userInfos = userInfoMapper.queryByName(userPhone, pageNum, pageSize);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
        return pageInfo;
    }

    @Override
    public void addUser(UserInfo userInfo) {
        int r = userInfoMapper.addUser(userInfo);
        if(r==1){
            System.out.println("用户注册成功!");
            return;
        }
        throw new AddException("用户注册失败!请检查输入内容是否合法。");
    }

    @Override
    public String updateHeaderPictureById(String userPig,String userId, Part part) throws IOException {
//        UserInfoMapper mapper = new UserInfoMapperImpl() ;
        //远程HFS上传地址
        String url = "http://192.168.43.193:8089/uploadPicture/" ;
        if(part.getSubmittedFileName() != null){
            int r = userInfoMapper.updateHeaderPicture(userPig,userId);
            HttpUtil.upload(url, part.getSubmittedFileName(), part.getInputStream());

            if(r==1){
                System.out.println("上传成功!");
            }
        }

        return url ;
    }

    @Override
    public void updateUser(UserInfo userInfo) {
        Integer integer = userInfoMapper.updateUser(userInfo);
        if(integer==1){
            System.out.println("修改成功");
            return;
        }
        throw new AddException("修改失败");
    }

    @Override
    public int updateUserPhone(String userPhone, String userId) {
        Integer r = userInfoMapper.updateUserPhone(userPhone, userId);
        if(r==1){
            System.out.println("修改成功");
            return r ;
        }
        throw new AddException("修改失败") ;
    }

    @Override
    public int updateUserPassword(String userPassword, String userId) {
        Integer r = userInfoMapper.updateUserPassword(userPassword, userId);
        if(r==1){
            System.out.println("修改成功");
            return r ;
        }
        throw new AddException("修改失败") ;
    }

    @Override
    public UserInfo queryUser(String userId) {
        UserInfoMapper mapper = new UserInfoMapperImpl() ;
        UserInfo userInfo = mapper.queryUser(userId);
        if(userInfo!=null){
            System.out.println("查询成功");
            return userInfo;
        }
        throw  new LoginException("查询失败") ;

    }
}
